package me.goldze.mvvmhabit.binding.viewadapter.image;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;
import com.bumptech.glide.request.target.SimpleTarget;
import com.bumptech.glide.request.target.Target;
import com.bumptech.glide.request.transition.Transition;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.BindingAdapter;
import me.goldze.mvvmhabit.R;
import me.goldze.mvvmhabit.utils.BlurTransformation;
import me.goldze.mvvmhabit.utils.KLog;
import me.goldze.mvvmhabit.utils.TransformationUtils;

/**
 * Created by goldze on 2017/6/18.
 */
public final class ViewAdapter {
    @BindingAdapter(value = {"url", "placeholderRes","errorRes","blur","isMix"}, requireAll = false)
    public static void setImageUri(final ImageView imageView, final String url, int placeholderRes, int errorRes, boolean blur, boolean isMix) {

       // KLog.d("url： "+url+" placeholderRes: "+placeholderRes+ " errorRes: "+errorRes+ " blur:"+blur+" isMix:"+isMix);
        if (placeholderRes == 0 ){
            placeholderRes = R.drawable.icon_img_loading;
        }
        if (errorRes == 0){
            errorRes = R.drawable.icon_img_loading_error;
        }

        RequestOptions options = new RequestOptions()
                .placeholder(placeholderRes)//图片加载出来前，显示的图片
                .fallback( placeholderRes) //url为空的时候,显示的图片
                .error(errorRes);//图片加载失败后，显示的图片


        if (!TextUtils.isEmpty(url)) {
            //使用Glide框架加载图片if

            if (blur){
                Glide.with(imageView.getContext())
                        .load(url)
                        .apply(options)
                        .apply(RequestOptions.bitmapTransform(new BlurTransformation(5,5)))
                        .into(imageView);
            }else {

                if (isMix){
//                    Glide.with(imageView.getContext()).applyDefaultRequestOptions(options).asBitmap().load(url)  .apply(options).into(new SimpleTarget<Bitmap>(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL) {
//                        @Override
//                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
//                            int imageWidth = resource.getWidth();
//                            int imageHeight = resource.getHeight();
//
//                            double sc  = (Double.valueOf(imageWidth)/+Double.valueOf(imageHeight));
//                            KLog.d("imageWidth:"+imageWidth+" imageHeight:"+imageHeight+"   mx:"+sc);
//                            if (sc< 0.75){
//                                int max =  imageView.getWidth();
//                                int height =  max * imageHeight / imageWidth;
//                                ViewGroup.LayoutParams para = imageView.getLayoutParams();
//                                para.height = height;
//                                para.width = max;
//                            }
//                            imageView.setImageBitmap(resource);
//                        }
//
//                    });

                    BitmapImageViewTarget viewTarget = new BitmapImageViewTarget(imageView) {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                            super.onResourceReady(resource, transition);
                            int imageWidth = resource.getWidth();
                            int imageHeight = resource.getHeight();
                            double sc  = (Double.valueOf(imageWidth)/+Double.valueOf(imageHeight));
                      //      KLog.d("imageWidth:BitmapImageViewTarget "+imageWidth+" imageHeight:"+imageHeight+"   mx:"+sc+" url:"+url);

                            if (imageHeight < imageWidth ){
                                int max =  imageView.getWidth();
                                int height =  max * imageHeight / imageWidth;
                                ViewGroup.LayoutParams para = imageView.getLayoutParams();
                                para.height = height;
                                para.width = max;
                                imageView.setLayoutParams(para);
                            }
                            imageView.setImageBitmap(resource);
                        }
                    };

                    Glide.with(imageView.getContext()).applyDefaultRequestOptions(options).asBitmap().load(url).apply(options).into(viewTarget);

                }else {
                    Glide.with(imageView.getContext())
                        .load(url)
                          .apply(options)
                        .into(imageView);
                }


            }

        }else {
          //  KLog.d("url： 鞍山市大多");
            Glide.with(imageView.getContext())
                    .load("")
                    .placeholder(placeholderRes)//图片加载出来前，显示的图片
                    .fallback( placeholderRes) //url为空的时候,显示的图片
                    .error(placeholderRes)//图片加载失败后，显示的图片
                    .into(imageView);
        }
    }
}

